<template>
    <!--根据用户ID查询用户权限-->
    <div class="UserPermission-container">
        <el-tree
                :data="data"
                :props="defaultProps"
                :default-expand-all="true"
                :check-strictly="false"
                node-key="id"
                v-loading="loading"
                highlight-current
                show-checkbox
                @node-click="handleNodeClick"
                ref="tree"
        ></el-tree>
    </div>
</template>

<script>
    import { mapGetters } from 'vuex'
    import { selectTree } from '@/api/upms/permission'
    import { getUserPermission } from '@/api/upms/user'
    export default {
        name: 'UserPermission',
        data() {
            return {
                loading: false,
                data: [],
                idKeys: [],
                defaultProps: {
                    label: 'name',
                    children: 'children'
                },
            }
        },
        created() {
            this.getDataTree()
        },
        computed: {
            ...mapGetters(['user'])
        },
        props: {
            userId: Number
        },
        methods: {
            back() {
                this.$router.back()
            },
            handleNodeClick(data) {
                this.$refs.tree.setChecked(data.id, true)
            },
            getDataTree() {
                this.loading = true
                selectTree().then(res => {
                    this.data = res.data
                    this.checkPermissions()
                    this.loading = false
                }).catch(error => {
                    this.loading = false
                    console.log(error)
                })
            },
            checkPermissions() {
                getUserPermission(this.userId).then(response => {
                    for(let item of response.data) {
                        this.$refs.tree.setChecked(item.id, true, true)
                    }
                })
            }
        },
        watch: {
            userId() {
                this.$refs.tree.setCheckedKeys([])
                this.checkPermissions()
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>